//
//  BaseViewController.m
//  TraderMobileKYi
//
//  Created by shijiezhao on 9/6/17.
//  Copyright © 2017年 com.yeezsoft.init. All rights reserved.
//

#import "EBMBaseViewController.h"
#import "SJNetStatusTool.h"

@interface EBMBaseViewController ()

@end

@implementation EBMBaseViewController


- (instancetype)init
{
    self = [super init];
    if(self)
    {
        
        
    }
    return self;
    
}


- (void)dealloc
{
    //  移除所有的注册通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    
    //  刷新请求网络请求
    [self baseRefreshRequestData];
    
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
    
    //  标题
    NSString *titleString = self.navigationItem.title;
    
    if(titleString)
    {
        //  埋页面     开始
        
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    
    //  收起键盘
    [self.view endEditing:YES];
    
    
    
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    NSString *titleString = self.navigationItem.title;
    
    if(titleString)
    {
        //  埋页面     结束
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  初始化变量
    [self baseInitData];
    
    // 创建视图
    [self baseInitView];
    
    
}

#pragma mark ----------初始化变量----------
//  初始化变量
- (void)baseInitData
{
    self.nvigationLetfBarButtonItemsMArray = [[NSMutableArray alloc]init];
    self.nvigationRightBarButtonItemsMArray = [[NSMutableArray alloc]init];
}

#pragma mark -- --------------------创建视图--------------------
// 创建视图
- (void)baseInitView
{
    // 设置属性
    [self baseSettingAttribute];
    
    // 创建左侧返回按钮
    [self baseCreateLeftBackButton];
}

#pragma mark ----------------------设置属性--------------------
// 设置属性
- (void)baseSettingAttribute
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
}


// 创建左侧返回按钮
- (void)baseCreateLeftBackButton
{
    [self baseCreateLeftBackButtonWithBackImage:@"Navigation_Back"];
}


// 创建左侧返回按钮
- (void)baseCreateLeftBackButtonWithBackImage:(NSString *)backImage
{
    
    
    if(self.navigationController.viewControllers.count>1)
    {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 0, 40, 40);
        [backButton setImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
        backButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 18);
        [backButton addTarget:self action:@selector(baseBackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
        
        self.navigationItem.leftBarButtonItem = barButtonItem;
    }
     
    
}




/// 创建控件    左侧按钮
/// @param title 标题
/// @param imageName 图片名称
/// @param buttonClickBlock 回调
- (void)baseCreateViewInNavigationBarLeftButtonWtihTitle:(NSString *)title
                                               imageName:(NSString *)imageName
                                        buttonClickBlock:(OperationBlock)buttonClickBlock
{
    //  回调
    self.navigationLeftButtonBlock = buttonClickBlock;
    
    
    //  按钮
    UIButton *navigationLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    navigationLeftButton.frame = CGRectMake(0, 0, 40, 40);
    navigationLeftButton.titleLabel.font = Font_14;
    [navigationLeftButton setTitleColor:RGBACOLOR16_fffff_80 forState:UIControlStateNormal];
    
    if(title)
    {
        [navigationLeftButton setTitle:title forState:UIControlStateNormal];
    }
    
    if(imageName)
    {
        [navigationLeftButton setImage:KImage(imageName) forState:UIControlStateNormal];
    }
    
    
    navigationLeftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -18, 0, 0);
    [navigationLeftButton addTarget:self action:@selector(navigationLeftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navigationLeftButton];
    
    [self.nvigationLetfBarButtonItemsMArray addObject:barButtonItem];
    
    self.navigationItem.leftBarButtonItems = self.nvigationLetfBarButtonItemsMArray;
    
    
}


/// 创建控件    右侧按钮
/// @param title 标题
/// @param imageName 图片名称
/// @param buttonClickBlock 回调
- (void)baseCreateViewInNavigationBarRightButtonWtihTitle:(NSString *)title
                                                imageName:(NSString *)imageName
                                         buttonClickBlock:(OperationBlock)buttonClickBlock
{
    //  回调
    self.navigationRightButtonBlock = buttonClickBlock;
    
    
   
    //  按钮
    UIButton *navigationRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    navigationRightButton.frame = CGRectMake(0, 0, 40, 40);
    navigationRightButton.titleLabel.font = Font_14;
    [navigationRightButton setTitleColor:RGBACOLOR16_fffff_80 forState:UIControlStateNormal];
    
    if(title)
    {
        [navigationRightButton setTitle:title forState:UIControlStateNormal];
    }
    
    if(imageName)
    {
        [navigationRightButton setImage:KImage(imageName) forState:UIControlStateNormal];
    }
    
    
    navigationRightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -18);
    [navigationRightButton addTarget:self action:@selector(navigationRightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navigationRightButton];
    
    [self.nvigationRightBarButtonItemsMArray addObject:barButtonItem];
    
    self.navigationItem.rightBarButtonItems = self.nvigationRightBarButtonItemsMArray;
    
    
}

#pragma mark ----------初始化网络请求----------

/// 刷新请求网络请求
- (void)baseRefreshRequestData
{
    
}


/// 网络重连刷新请求网络请求
- (void)baseNetWorkRefreshRequestData
{
    //  初始化网络请求
    [self requestData];
}


//  初始化网络请求
- (void)requestData
{
    
}


#pragma mark -- --------------------方法--------------------

//  设置导航栏的颜色方法      父类的方法
- (UIStatusBarStyle)preferredStatusBarStyle
{
    //  UIStatusBarStyleDefault         黑色
    //  UIStatusBarStyleLightContent    白色
    return UIStatusBarStyleLightContent;
}

//  隐藏返回按钮
- (void)baseHideBackButton
{
    self.navigationItem.leftBarButtonItem = nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //  收起键盘
    [self.view endEditing:YES];
}


/// 登录成功
- (void)loginRefresh
{
    
}

/// 退出刷新
- (void)logoutRefresh
{
    
}



#pragma mark -- --------------------响应事件--------------------
//  返回按钮
- (void)baseBackButtonClick:(UIButton *)button
{
    //  收起键盘
    [self.view endEditing:YES];
    
    [self.navigationController popViewControllerAnimated:YES];
}


//  导航栏左侧按钮点击
- (void)navigationLeftButtonClick:(UIButton *)sender
{
    //  收起键盘
    [self.view endEditing:YES];
    
    if(self.navigationLeftButtonBlock)
    {
        self.navigationLeftButtonBlock();
            
    }
}


//  导航栏右侧按钮点击
- (void)navigationRightButtonClick:(UIButton *)sender
{
    //  收起键盘
    [self.view endEditing:YES];
    
    if(self.navigationRightButtonBlock)
    {
        self.navigationRightButtonBlock();
            
    }
}






@end
